package com.dao;

import com.bean.User;
import com.db.DBUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class UserDaoImp implements UserDao {
    @Override
    public boolean isUser(User user) {
        //sql语句
        String sql="select * from user where name=? and pwd=?";
        //从数据库连接池获取连接对象
        Connection conn= DBUtils.getConn();
        //创建预处理
        PreparedStatement p;
        try {
            p=conn.prepareStatement(sql);//获取预处理对象
            p.setString(1, user.getName());
            p.setString(2, user.getPwd());
            ResultSet rs= p.executeQuery();//执行sql查询指令,返回result
            return rs.next(); //存在用户,返回true
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;//不存在用户,返回false
    }

    @Override
    public boolean checkUsernameExist(String username) {
        String sql = "select * from user where name=?";
        Connection conn= DBUtils.getConn();
        PreparedStatement p;
        try {
            p=conn.prepareStatement(sql);//获取预处理对象
            p.setString(1, username);
            ResultSet resultSet =  p.executeQuery();
            return resultSet.next();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }
}
